package com.chs.recoginition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chs.recoginition.core.exception.ApplicationException;
import com.chs.recoginition.core.exception.constant.ExceptionConstant;
import com.chs.recoginition.core.utils.BeanMapper;
import com.chs.recoginition.mapper.InvitationContentMapper;
import com.chs.recoginition.pojo.IdentificationContent;
import com.chs.recoginition.pojo.InvitationContent;
import com.chs.recoginition.service.IIdentificationContentService;
import com.chs.recoginition.service.IInvitationContentService;
import com.chs.recoginition.vo.PageRespVo;
import com.chs.recoginition.vo.reqvo.InvitationContentPageVo;
import com.chs.recoginition.vo.respVo.ImageRecognitionRespVo;
import com.chs.recoginition.vo.respVo.InvitationContentRespVo;
import com.chs.recoginition.vo.respVo.UserRespVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 帖子 服务实现类
 * </p>
 * @author CHS
 * @since 2023-04-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InvitationContentServiceImpl extends ServiceImpl<InvitationContentMapper, InvitationContent> implements IInvitationContentService {
    @Resource
    private IIdentificationContentService iIdentificationContentService;
    @Override
    public PageRespVo<InvitationContentRespVo> findPage(InvitationContentPageVo reqVo) {
        PageRespVo<InvitationContentRespVo> result = baseMapper.findPage(reqVo);
        for(int i=0;i<result.getRows().size();i++) {
            String id = result.getRows().get(i).getIdentificationId();
            ImageRecognitionRespVo byId = iIdentificationContentService.findById(id);
            result.getRows().get(i).setImageRecognitionRespVo(byId);
        }
        return result;
    }

    @Override
    public InvitationContentRespVo findById(Integer id) {
        InvitationContent invitationContent = baseMapper.selectById(id);
        if(invitationContent != null) {
        InvitationContentPageVo req = new InvitationContentPageVo();
        req.setId(id);
        PageRespVo<InvitationContentRespVo> page = baseMapper.findPage(req);
            for(int i=0;i<page.getRows().size();i++) {
                String identificationId = page.getRows().get(i).getIdentificationId();
                ImageRecognitionRespVo byId = iIdentificationContentService.findById(identificationId);
                page.getRows().get(i).setImageRecognitionRespVo(byId);
            }
        if(!page.getRows().isEmpty()) {
            return page.getRows().get(0);
        }
        }
        return null;

    }

    @Override
    public void save(InvitationContentRespVo reqVo, UserRespVo userRespVo) {

        InvitationContent map = BeanMapper.map(reqVo, InvitationContent.class);
        ImageRecognitionRespVo imageRecognitionRespVo = reqVo.getImageRecognitionRespVo();
        if(imageRecognitionRespVo != null) {
            String identificationId = iIdentificationContentService.save(imageRecognitionRespVo, userRespVo);
            map.setIdentificationId(identificationId);
        }
        map.setCreatedBy(userRespVo.getUserId());
        map.setCreatedTime(LocalDateTime.now());
        baseMapper.insert(map);

    }

    @Override
    public void modify(InvitationContent reqVo, UserRespVo userRespVo) {
        if(reqVo.getId() == null) {
            throw new ApplicationException(ExceptionConstant.ENTITY_NOT_EXISTS);
        }
        InvitationContent invitationContent = baseMapper.selectById(reqVo.getId());
        if(invitationContent == null) {
            throw new ApplicationException(ExceptionConstant.ENTITY_NOT_EXISTS);
        }
        reqVo.setUpdatedBy(userRespVo.getUserId());
        reqVo.setUpdatedTime(LocalDateTime.now());
        baseMapper.updateById(reqVo);
    }

    @Override
    public void remove(String idList) {
        List<Integer> ids = Arrays.stream(idList.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        //删除识别内容
        List<InvitationContent> invitationContents = baseMapper.selectBatchIds(ids);
        String iIdentifications = invitationContents.stream().map(v -> v.getIdentificationId().toString()).collect(Collectors.joining(","));
        iIdentificationContentService.remove(iIdentifications);
        //删除帖子
        baseMapper.deleteBatchIds(ids);
    }
}
